package com.systemmanger.controller;

import com.bean.WmsTCustomer;
import com.bean.WmsTUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.systemmanger.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @AUTHOR(闫晨航)
 * @date2021-12-02 21:01
 */
@Controller
public class CustomerController {
    @Autowired
    CustomerService customerService;

//    @RequestMapping("test.do")
//    public void test(HttpServletResponse response) throws IOException {
////        return "home";
//        response.sendRedirect("newhome.html");
//    }
    //客户管理页面
    @RequestMapping("xitong_customer.do")
    public String getAllCustomers(HttpServletRequest request){
        int pageNum = 1;
        int pageRows = 15;
        PageHelper.startPage(pageNum,pageRows);
        List<WmsTCustomer> wmsTCustomers = customerService.getAllCustomers();
        PageInfo<WmsTCustomer> pageInfo = new PageInfo<>(wmsTCustomers);
        List<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(10);
        list.add(15);
        request.getSession().setAttribute("pageRows",pageRows);
        request.getSession().setAttribute("list",list);
        if(pageInfo!=null){
            request.getSession().setAttribute("pageInfo",pageInfo);
        }
        return "xitong/xitong_customer";
    }
    //客户页面分页操作
    @RequestMapping("xitong_selectCustomer.do")
    public String selectCustomer(int pageRows, String pageNum, String custID, String custName, String custIC, HttpServletRequest request) throws UnsupportedEncodingException {
        Map<String,Object> map = new HashMap<>();
        request.getSession().removeAttribute("custName");
        request.getSession().removeAttribute("custID");
        request.getSession().removeAttribute("custIC");
        int pageNum1 = 1;
        if(custName!=null){
            if(!custName.equals("")){
//                custName = new String(custName.getBytes("ISO-8859-1"),"utf-8");
                System.out.println(custName);
                map.put("custName",custName);
                request.getSession().setAttribute("custName",custName);
            }
        }
        if(custID!=null){
            if(!custID.equals("")){
                map.put("custID",custID);
                request.getSession().setAttribute("custID",custID);
            }
        }
        if(custIC!=null){
            if(!custIC.equals("")){
                map.put("custIC",custIC);
                request.getSession().setAttribute("custIC",custIC);
            }
        }
        if(pageNum!=null){
            pageNum1=Integer.parseInt(pageNum);
        }
        request.getSession().setAttribute("pageRows",pageRows);
        PageHelper.startPage(pageNum1,pageRows);
        List<WmsTCustomer> wmsTCustomers = customerService.getCustomers(map);
        PageInfo<WmsTCustomer> pageInfo = new PageInfo<>(wmsTCustomers);

        if(pageInfo!=null){
//            System.out.println(pageInfo);
            request.getSession().setAttribute("pageInfo",pageInfo);
        }
        return "xitong/xitong_customer";
    }
    //获取一个customer
    @RequestMapping("xitong_getOnecust.do")
    @ResponseBody
    public WmsTCustomer getOnecust(String custID,HttpServletRequest request){
        System.out.println(custID);
        WmsTCustomer wmsTCustomer = customerService.getOneCus(custID);
        System.out.println(wmsTCustomer);
        request.getSession().setAttribute("wmsTCustomer",wmsTCustomer);
        return wmsTCustomer;
    }
    //修改customer
    @RequestMapping("xitong_update.do")
    public void updateCustomer(WmsTCustomer wmsTCustomer,String edit,HttpServletResponse response,HttpServletRequest request) throws IOException {
        System.out.println(wmsTCustomer);
        System.out.println(edit);
        response.setCharacterEncoding("gbk");
        if(edit.equals("delete")){
            customerService.deleteBycustomer(wmsTCustomer);
            response.getWriter().println("<script>alert('删除成功')</script>");
        }else if(edit.equals("edit")){
            customerService.updateByCustomer(wmsTCustomer);
            response.getWriter().println("<script>alert('修改成功')</script>");
        }
        request.getSession().removeAttribute("wmsTCustomer");

        response.sendRedirect("xitong_customer.do");
    }

}
